package com.chinabank.service;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinabank.dao.AccountDao;
import com.chinabank.exception.NoSuchAccountException;
import com.chinabank.model.Account;
import com.chinabank.util.MD5;


@Service(value = "accountService")
public class AccountService {

	private AccountDao accountDao;

	public AccountDao getAccountDao() {
		return accountDao;
	}

	@Resource
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Transactional
	public void addAccount(Account account) {
		accountDao.add(account);
	}

	@Transactional
	public void delAccount(Account account) {
		accountDao.delete(account);
	}

	@Transactional
	public void updateAccount(Account account) {
		accountDao.update(account);
	}

	@Transactional
	public Account getAccount(Serializable id) {
		return accountDao.get(Account.class, id);
	}

	@Transactional
	public Account getAccount(String username, String password)
			throws NoSuchAccountException {
		String encptPassword = MD5.getMD5Code(password);
		Account account = accountDao.get(username, encptPassword);
		return account;
	}

	@Transactional
	public List<Account> listAccount() {
		return accountDao.listAccount();
	}

	@Transactional
	public void delAccount(Serializable id) {
		accountDao.delAccount(id);
	}
}
